﻿using BZOrderPrint.DAL;
using BZOrderPrint.DAL.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Migrations;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BZOrderPrint.BLL.Helper;
using System.Collections;
using static BZOrderPrint.DAL.AppEnum;

namespace BZOrderPrint
{
    public partial class FrmShopModify : Form
    {
        private BzShopEntity _entity;
        private BZContext context;

        public FrmShopModify(BzShopEntity entity)
        {
            InitializeComponent();
            _entity = entity;
            context = new BZContext(ConfigHelper.ConnString);
            Init();
            ShopTypeInit();
        }

        private void Init()
        {
            if (_entity.Id.Equals(0))
            {
                Text = "新增门店";
            }
            else
            {
                Text = "修改门店";
                textBox1.Text = _entity.ShopCode;
                textBox1.Enabled = false;
                textBox2.Text = _entity.ShopName;
                textBox2.Enabled = false;
                textBox3.Text = _entity.Province;
                textBox3.Enabled = false;
                textBox4.Text = _entity.City;
                textBox4.Enabled = false;
                textBox6.Text = _entity.Area;
                textBox6.Enabled = false;
                textBox5.Text = _entity.Street;
                textBox5.Enabled = false;
                textBox8.Text = _entity.Address;
                textBox8.Enabled = false;
                textBox9.Text = _entity.Postcode;
                textBox7.Text = _entity.Phone;
                textBox11.Text = _entity.ShopAdmin;
                foreach (AppEnum.ShopType shopType in Enum.GetValues(typeof(AppEnum.ShopType)))
                {
                    comboBox1.Items.Add(shopType.GetDescription());
                    if (_entity.ShopType == (int) shopType)
                    {
                        comboBox1.SelectedItem = shopType.GetDescription();
                        comboBox1.Enabled = false;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("网点编码不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("网点名称不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("所在省份不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("所在城市不能为空!");
                return;
            }
            //if (string.IsNullOrEmpty(textBox5.Text))
            //{
            //    MessageBox.Show("所在区不能为空!");
            //    return;
            //}
            if (string.IsNullOrEmpty(textBox6.Text))
            {
                MessageBox.Show("所在区不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox7.Text))
            {
                MessageBox.Show("具体地址不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox8.Text))
            {
                MessageBox.Show("邮编不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox9.Text))
            {
                MessageBox.Show("电话不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(textBox11.Text))
            {
                MessageBox.Show("店长不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(comboBox1.SelectedText))
            {
                MessageBox.Show("门店类型不能为空!");
                return;
            }

            try
            {
                foreach (AppEnum.ShopType shopType in Enum.GetValues(typeof(AppEnum.ShopType)))
                {
                    if (shopType.GetDescription().Equals(comboBox1.SelectedText))
                    {
                        _entity.ShopType = (int) shopType; 
                    }
                }

                if (_entity.Id.Equals(0))
                {
                    _entity.ShopCode = textBox1.Text;
                    _entity.ShopName = textBox2.Text;
                    _entity.Province = textBox3.Text;
                    _entity.City = textBox4.Text;
                    _entity.Area = textBox5.Text;
                    _entity.Street = textBox6.Text;
                    _entity.Address = textBox7.Text;
                    _entity.Postcode = textBox8.Text;
                    _entity.Phone = textBox9.Text;
                    _entity.ShopAdmin = textBox11.Text;
                    context.bzShopEntities.Add(_entity);
                    context.SaveChanges();
                    MessageBox.Show("添加成功!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    _entity.Postcode = textBox8.Text;
                    _entity.Phone = textBox9.Text;
                    _entity.ShopAdmin = textBox11.Text;
                    context.bzShopEntities.AddOrUpdate(_entity);
                    context.SaveChanges();
                    MessageBox.Show("修改成功!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ShopTypeInit()
        {
            var list = Enum.GetValues(typeof(ShopType));
            List<DictionaryEntry> dataList = new List<DictionaryEntry>();
            foreach (var item in list)
            {
                DictionaryEntry info = new DictionaryEntry { Value = (int)(ShopType)item, Key = EnumHelper.GetDescription((ShopType)item) };
                dataList.Add(info);
            }
            var indexItem = new DictionaryEntry { Value = "-1", Key = "全部" };
            dataList.Add(indexItem);
            comboBox1.DataSource = dataList;
            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";
            comboBox1.SelectedItem = indexItem;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmShopModify_Load(object sender, EventArgs e)
        {

        }
    }
}